<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@page import="model.BorrowedItem"%>
<%@page import="dao.ItemManager"%>
<%@page import="dao.Utils"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Arrays"%>
<%@page import="model.ItemType"%>
<%@page import="model.Item"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Items</title>
</head>
<body>
	<%@include file='home.jsp'%>
	<%
	    String message = (String) session.getAttribute("message");
	    if (message != null)
	    {
			out.println(message);
			session.setAttribute("message", null);
	    }
	    /*
	    @SuppressWarnings("unchecked")
	    ArrayList<Item> items = (ArrayList<Item>) session
	    	    .getAttribute("items");
	     */
	    ItemManager manager = new ItemManager();
	    List<Item> items = manager.fetchOwnFreeItems(user.getUsername());
	    if (items != null && items.size() > 0)
	    {
	%>
	<h3>Available own items</h3>
	<table border=1>
		<tr>
			<th>ID</th>
			<th>Title</th>
			<th>Author</th>
			<th>Published</th>
			<th>Type</th>
			<th>Period</th>
			<th>Available</th>
		</tr>
		<%
		    for (Item item : items)
				{
		%>
		<tr>
			<td><%=item.getId()%></td>
			<td><%=item.getTitle()%></td>
			<td><%=item.getAuthor()%></td>
			<td><%=item.getPublishDate()%></td>
			<td><%=item.getType()%></td>
			<td><%=item.getMaxPeriod()%> days</td>
			<td><%=Utils.checkAvailable(item.getState())%></td>
			<td><form action="addEditItem.jsp" method="post">
					<input type="hidden" , name="itemId" value="<%=item.getId()%>" />
					<input type="submit" value="Edit" />
				</form></td>
			<td><form action="borrowItem.jsp" method="post">
					<input type="hidden" , name="itemId" value="<%=item.getId()%>" />
					<input type="submit" value="Borrow" />
				</form></td>
		</tr>
		<%
		    }
		%>
	</table>
	<%
	    }
	    List<BorrowedItem> borrowedItems = manager
			    .fetchOwnBorrowedItems(user.getUsername());
	    if (borrowedItems != null && borrowedItems.size() > 0)
	    {
	%>
	<h3>Borrowed own items</h3>
	<table border=1>
		<tr>
			<th>ID</th>
			<th>Title</th>
			<th>Author</th>
			<th>Published</th>
			<th>Type</th>
			<th>Borrower</th>
			<th>Borrow date</th>
			<th>Exp. return date</th>
		</tr>
		<%
		    for (BorrowedItem borrowedItem : borrowedItems)
				{
				    Item item = borrowedItem.getItem();
		%>
		<tr>
			<td><%=item.getId()%></td>
			<td><%=item.getTitle()%></td>
			<td><%=item.getAuthor()%></td>
			<td><%=item.getPublishDate()%></td>
			<td><%=item.getType()%></td>
			<td><%=borrowedItem.getBorrower()%></td>
			<td><%=borrowedItem.getBorrowDate()%></td>
			<td><%=borrowedItem.getExpectedReturnDate()%></td>
			<td><form action="returnItem" method="post">
					<input type="hidden" , name="borrowedItemId"
						value="<%=borrowedItem.getId()%>" /> <input type="submit"
						value="Return" />
				</form></td>
		</tr>
		<%
		    }
		%>
	</table>

	<%
	    }
	%>
	<br />
	<br />
</body>
</html>